package com.orhon.smartcampus.modules.systemctl.fetcher;


import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentUserRelationDao;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartmentUserRelation;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDuties;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDepartment;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDepartmentUsers;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDuty;
import com.orhon.smartcampus.modules.systemctl.mm.DepartmentMapper;
import com.orhon.smartcampus.modules.systemctl.mm.DepartmentUsersRecordMapper;
import com.orhon.smartcampus.modules.systemctl.mm.DutyMapper;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.teacher.gqlModel.GQLTeacher;
import com.orhon.smartcampus.modules.teacher.mm.TeacherMapper;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class UsersNotUnassignedDepartmentFetcher implements DataFetcher {


	@Autowired
	TInformationDao tInformationDao;

    @Override
    public Object get(DataFetchingEnvironment env) throws Exception {

		List<GQLTeacher> GQLTeacher = new ArrayList<>();


		GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
		String hql = "from TInformation where deleted_at = null and user_id not in (select userUsersByUserId from OrgDepartmentUserRelation group by userUsersByUserId) ";

		if(env.containsArgument("condition")) {
			HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			if(map.get("idcard")!=null) {
				hql += "and idcard " + Util.decide(map.get("idcard"));
			}
			if(map.get("user_id")!=null) {
				hql += "and user_id "+ Util.decide(map.get("user_id"));
			}
			if(map.get("duty_id")!=null) {
				hql += "and duty_id "+ Util.decide(map.get("duty_id"));
			}
		}
		List<TInformation> list = tInformationDao.find(hql,null,page.getPage(),page.getLimit());


		for (TInformation item : list) {
			GQLSchool gqlSchool = null;
			if (env.getSelectionSet().contains("data/school")){
				Schools schools = item.getBaseSchoolsBySchoolId();
				gqlSchool = SchoolMapper.MAPPER.toSchool(schools);
			}

			GQLUser gqlUser = null;
			if (env.getSelectionSet().contains("data/user")){
				Users users = item.getUserUsersByUserId();
				gqlUser = UserMapper.MAPPER.mapUser(users);
			}

			GQLTeacher teacher = TeacherMapper.MAPPER.toTeacher(item);
			teacher.setUser(gqlUser);
			teacher.setSchool(gqlSchool);
			GQLTeacher.add(teacher);
		}

		Long total = 0L;
		if (env.getSelectionSet().contains("total")){
			total = tInformationDao.getTotalCount(hql);
		}
		return Result.ok().put("data", GQLTeacher).put("total", total);
    }



}
